from gmssl.sm4 import CryptSM4,SM4_ENCRYPT,SM4_DECRYPT
import binascii
from tkinter import ttk
import tkinter as tk 
from tkinter import scrolledtext,messagebox


crypt_sm4=CryptSM4()
iv='aojcusv'

def tobyte(st):
    strs=st.encode('utf-8')
##    str_s=binascii.b2a_hex(strs)
##    strs=binascii.b2a_base64(strs)
    str_s=binascii.hexlify(strs)
    return str_s

def tost(byt):
    s=binascii.unhexlify(byt).decode('utf-8')
    return s


def key16(st):
    i=65
    while len(st)%16!=0:
        st+=bytes(chr(i),encoding='utf-8')
        i+=1
    s1=st[:-6]
    e1=st[-6:]
    st=e1+s1
    return st



def sm4_encrypt(k,iv,st):
    crypt_sm4.set_key(k,SM4_ENCRYPT)
    enval=crypt_sm4.crypt_cbc(iv,st)
    return binascii.b2a_hex(enval).decode('utf-8')

def sm4_decrypt(k,iv,enst):
    enst=binascii.a2b_hex(enst)
    crypt_sm4.set_key(k,SM4_DECRYPT)
    deval=crypt_sm4.crypt_cbc(iv,enst)
    return deval

def btn_encrypt():
    global scrtext_str
    global ykey
    global scrtext_see
    getstr=scrtext_str.get('1.0',tk.END)
    getkey=ykey.get()
    if not getstr.strip('\n'):
        return
    if not getkey:
        return
    enst=sm4_encrypt(key16(tobyte(getkey)),key16(tobyte(iv)),tobyte(getstr))
    scrtext_see.insert(tk.END,enst)
    scrtext_see.insert(tk.END,'\n')

def btn_decrypt():
    global scrtext_str
    global ykey
    global scrtext_see
    getstr=scrtext_str.get('1.0',tk.END)
    getkey=ykey.get()
    if not getstr.strip('\n'):
        return
    if not getkey:
        return
    try:
        dest=sm4_decrypt(key16(tobyte(getkey)),key16(tobyte(iv)),getstr.strip("\n").encode('utf-8'))
        scrtext_see.insert(tk.END,tost(dest))
        scrtext_see.insert(tk.END,'\n')
    except:
        print('err')
        

def btn_delete():
    global scrtext_see
    d=messagebox.askyesno('提示','是否要清空下框?')
    if d:
        scrtext_see.delete(1.0,tk.END)

def btn_write():
    global ykey
    global scrtext_see
    getstr=scrtext_see.get('1.0',tk.END)
    getkey=ykey.get()
    
    if not getstr.strip('\n'):
        return
    if not getkey:
        return
    try:
        with open('sm4crypt.txt','a') as f:
            f.write(getkey+' 密文:'+getstr)
        messagebox.showinfo('提示','已录入sm4crypt.txt')
    except:
        pass

def sm4_gui():
    global scrtext_str
    global ykey
    global scrtext_see
    win=tk.Tk()
    win.title('SM4加解密')
    win.geometry('600x400')
    label_key=ttk.Label(win,text='密钥:')
    label_key.place(x=10,y=10)
    ykey=tk.StringVar()
    entry_key=ttk.Entry(win,textvariable=ykey)
    entry_key.place(x=90,y=10)
    label_str=ttk.Label(win,text='加/解密字符:')
    label_str.place(x=10,y=40)
    scrtext_str=scrolledtext.ScrolledText(win,width=62,height=10)
    scrtext_str.place(x=90,y=40)
    btn_en=ttk.Button(win,text='加密',command=btn_encrypt)
    btn_en.place(x=30,y=180)
    btn_de=ttk.Button(win,text='解密',command=btn_decrypt)
    btn_de.place(x=130,y=180)
    btn_del=ttk.Button(win,text='清空',command=btn_delete)
    btn_del.place(x=260,y=180)
    btn_w=ttk.Button(win,text='记录',command=btn_write)
    btn_w.place(x=360,y=180)
    scrtext_see=scrolledtext.ScrolledText(win,width=76,height=10)
    scrtext_see.place(x=10,y=230)
    win.mainloop()
    

if __name__=='__main__':

    sm4_gui()




    
